package com.sgb.knowledgeshare.view.fragment;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareClassifyListBinding;
import com.swgk.core.view.entity.VideoTypeListEntity;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.knowledgeshare.view.adapter.ShareClassifyAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareClassifyListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.VideoListEntity;

import java.io.Serializable;
import java.util.List;

/**
 * Created by fq
 * on 2020/9/9
 * 分享-分类-viewpage列表数据
 */
public class ShareClassifyListFragment  extends AppLazyFragment {
    ShareClassifyListModel viewmode;
    private static  final String CLASSIFY = "fX_classify";
    private FragmentShareClassifyListBinding binding;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private ShareClassifyAdapter shareclassifyadapter;
    private int page=1;
    private List<VideoBean> listvideo;
    private boolean isLoadMore;
    private List<VideoTypeListEntity.ChildrenBean> childrenbean;
    private String no = "";
    private int pages;

    public static ShareClassifyListFragment newInstance(String goodsType,List<VideoTypeListEntity.ChildrenBean> children) {
        ShareClassifyListFragment fragment = new ShareClassifyListFragment();
        Bundle args = new Bundle();
        args.putString(CLASSIFY, goodsType);
        args.putSerializable("childrenbean", (Serializable) children);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_classify_list, container, false);
        viewmode = new ViewModelProvider(this).get(ShareClassifyListModel.class);
        viewmode.setActivity(ShareClassifyListFragment.this);
        binding.setViewModel(viewmode);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        if (TextUtils.isEmpty(getArguments().getString(CLASSIFY))){
            binding.llTitle.setVisibility(View.GONE);
        }
        childrenbean = (List<VideoTypeListEntity.ChildrenBean>) getArguments().getSerializable("childrenbean");
        if (childrenbean!=null&&childrenbean.size()>0){
            binding.llTitle.setVisibility(View.VISIBLE);
            for (int i = 0; i < childrenbean.size(); i++) {
                binding.tablayout.addTab( binding.tablayout.newTab().setText(childrenbean.get(i).getName()));
            }
        }else {
            binding.llTitle.setVisibility(View.GONE);
        }
        binding.tablayout.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (childrenbean!=null&&childrenbean.size()>0){
                    no = childrenbean.get(position).getNo();
                    shareclassifyadapter.getDatas().clear();
                    shareclassifyadapter.notifyDataSetChanged();
                    page =1;
                    initdata(page,no);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        shareclassifyadapter = new ShareClassifyAdapter(getActivity(), null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(shareclassifyadapter);
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager( 2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //防止item位置互换
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        binding.lrecyclerview.setLayoutManager(layoutManager);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(getContext())
                .setHorizontal(R.dimen.dp1)
                .setVertical(R.dimen.dp1)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                shareclassifyadapter.getDatas().clear();
                page = 1;
                initdata(page,no);
                mLRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                if (page<=pages){
                    if (TextUtils.isEmpty(no)){
                        if (childrenbean!=null&&childrenbean.size()>0){
                            no = childrenbean.get(0).getNo();
                            viewmode.getVideoTypeList(page,no);
                        }else {
                            viewmode.getVideoTypeList(page,getArguments().getString(CLASSIFY)+"");
                        }
                    }else {
                        viewmode.getVideoTypeList(page,no);
                    }
                }else {
                    binding.lrecyclerview.setNoMore(true);
                }
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
           //跳转到播放列表数据
                // VideoPlayUserInfoActivity.class
                Intent intent = new Intent(getActivity(), PlayListActivity.class );
                intent.putExtra(PlayListActivity.VIDEOTYPE,"fX_classify");
                intent.putExtra(PlayListActivity.VIDEOTITLE,TextUtils.isEmpty(no)? no:getArguments().getString(CLASSIFY)+"");
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) listvideo);
                intent.putExtra(PlayListActivity.PSO,position);
                intent.putExtra(PlayListActivity.PAGE,page);
                getActivity().startActivity(intent);
            }
        });

    }
    @Override
    protected void onLazyLoad() {
        if (TextUtils.isEmpty(no)){
            if (childrenbean!=null&&childrenbean.size()>0){
                no =  childrenbean.get(0).getNo();
            }else {
                no = getArguments().getString(CLASSIFY)+"";
            }
            initdata(page,no);
        }else {
            initdata(page,no);
        }
    }

    private void initdata(int pages,String twoNo) {
        isLoadMore = false;
        viewmode.getVideoTypeList(pages,twoNo);
    }


    //返回对应类型的列表数据
    public void setVideoTypeList(VideoListEntity data) {
        pages = data.getPages();
        List<VideoBean> records = data.getRecords();
        if (null == records) {
            return;
        }
        binding.lrecyclerview.refreshComplete(records.size());
        if (isLoadMore){
            if (records.size()>0){
                shareclassifyadapter.addDatas(records);
            }else {
                binding.lrecyclerview.setNoMore(true);
                MToast.showToast(getContext(),"没有视频啦");
            }
        }else {
            if (records.size()>0){
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.rlNodeta.setVisibility(View.GONE);
                shareclassifyadapter.setDatas(records);
            }else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.rlNodeta.setVisibility(View.VISIBLE);
                shareclassifyadapter.setDatas(null);
            }

        }
        this.listvideo = shareclassifyadapter.getDatas();
    }
}
